package com.imooc.controller;

import com.imooc.config.SellerConfig;
import com.imooc.dto.OrderDTO;
import com.imooc.enums.ResultEnum;
import com.imooc.exception.SellException;
import com.imooc.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * FileName: SellerOrderController.java
 * Description: 卖家端controller
 *
 * @author HuangRI(874668739 @ qq.com)
 * @version 1.0
 * @date 2020/12/28
 */
@Controller
@Slf4j
@RequestMapping("/seller/order")
public class SellerOrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 订单列表
     *
     * @param page 页码，从1开始
     * @param size 每页显示最大数量
     * @return ModelAndView
     */
    @GetMapping("/list")
    public ModelAndView list(@RequestParam(value = "page", defaultValue = "1") Integer page,
                             @RequestParam(value = "size", defaultValue = "#{sellerConfig.page.size}") Integer size,
                             Map<String, Object> model) {
        PageRequest pageRequest = PageRequest.of(page - 1, size);
        Page<OrderDTO> pageModel = this.orderService.findList(pageRequest);
        model.put("pageModel", pageModel);
        model.put("curPage", page);
        model.put("size", size);
        return new ModelAndView("/order/list", model);
    }

    /**
     * 取消订单
     *
     * @param orderId 订单ID
     * @return
     */
    @GetMapping("/cancel")
    public ModelAndView cancel(@RequestParam("orderId") String orderId,
                               @RequestParam(value = "redirectUrl", required = false) String redirectUrl,
                               Map<String, Object> model) {
        try {
            log.info("【卖家端取消订单】orderId={}", orderId);
            OrderDTO orderDTO = this.orderService.findOne(orderId);
            this.orderService.cancel(orderDTO);
            model.put("msg", ResultEnum.ORDER_CANCEL_SUCCESS.getMessage());
            if (StringUtils.isBlank(redirectUrl)) {
                model.put("url", "/sell/seller/order/list");
            } else {
                model.put("url", redirectUrl);
            }
            return new ModelAndView("/common/success", model);
        } catch (SellException e) {
            log.error("【卖家端取消订单】发生异常", e);
            model.put("msg", e.getMessage());
            model.put("url", "/sell/seller/order/list");
            return new ModelAndView("/common/error", model);
        }
    }

    /**
     * 订单详情
     * @param orderId
     * @param model
     * @return
     */
    @GetMapping("/detail")
    public ModelAndView detail(@RequestParam("orderId") String orderId,
                               Map<String, Object> model) {
        try {
            log.info("【卖家端查询订单详情】orderId={}", orderId);
            OrderDTO orderDTO = this.orderService.findOne(orderId);
            model.put("orderDTO", orderDTO);
            return new ModelAndView("/order/detail", model);
        } catch (SellException e) {
            log.error("【卖家端查询订单详情】发生异常", e);
            model.put("msg", e.getMessage());
            model.put("url", "/sell/seller/order/list");
            return new ModelAndView("/common/error", model);
        }
    }

    /**
     * 完结订单
     * @param orderId
     * @param model
     * @return
     */
    @GetMapping("/finish")
    public ModelAndView finish(@RequestParam("orderId") String orderId,
                               Map<String, Object> model) {
        try {
            log.info("【卖家端完结订单】orderId={}", orderId);
            OrderDTO orderDTO = this.orderService.findOne(orderId);
            this.orderService.finish(orderDTO);
            model.put("msg", ResultEnum.ORDER_FINISH_SUCCESS.getMessage());
            model.put("url", "/sell/seller/order/detail?orderId=" + orderId);
            return new ModelAndView("/common/success", model);
        } catch (SellException e) {
            log.error("【卖家端完结订单】发生异常", e);
            model.put("msg", e.getMessage());
            model.put("url", "/sell/seller/order/list");
            return new ModelAndView("/common/error", model);
        }
    }
}
